{include file='public:header-css'/}
<body>
<div class="lyear-layout-web">
    <div class="lyear-layout-container">
        <!--左侧导航-->
        {include file='public:left-menu'/}
        <!--End 左侧导航-->
        <!--头部信息-->
        {include file='public:nav-header' title="养殖管理" subTitle="添加编辑监控"/}
        <!--End 头部信息-->

        <!--页面主要内容-->
        <main class="lyear-layout-content">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-lg-12">
                        <div class="card">
                            <div class="card-body">
                                <form method="post" id="submit-form" class="row">
                                    <div class="form-group col-md-12">
                                        <label class="form-title"><em class="require">*</em>监控名称</label>
                                        <input type="text" class="form-control" name="name" value="{$info.name}" placeholder="请输入监控名称"/>
                                        <span class="error" id="err_name"></span>
                                    </div>
                                    <div class="form-group col-md-12">
                                        <label class="form-title"><em class="require">*</em>分类名称</label>
                                        <select id="type" class="input-group-select" name="c_id" style="width:100%">
                                            <option value="">请选择分类名称</option>
                                            {volist name="class" id="class"}
                                            <option value="{$class.id}" {if $info.c_id==$class.id}selected{/if}>{$class.name}</option>
                                            {/volist}
                                        </select>
                                        <span class="error" id="err_c_id"></span>
                                    </div>
                                    <div class="form-group col-md-12">
                                        <label class="form-title"><em class="require">*</em>设备序列号</label>
                                        <input type="text" class="form-control" name="serial" value="{$info.serial}" placeholder="请输入设备序列号"/>
                                        <span class="error" id="err_serial"></span>
                                    </div>
                                    <div class="form-group col-md-12">
                                        <label class="form-title"><em class="require">*</em>设备验证码</label>
                                        <input type="text" class="form-control" name="code" value="{$info.code}" placeholder="请输入设备验证码"/>
                                        <span class="error" id="err_code"></span>
                                    </div>
                                    <div class="form-group col-md-6">
                                        <label><em class="require">*</em>主图</label>
                                        <div class="upload-row">
                                            <img class="upload-img" id="img"
                                                 src="{$info.img|default='__STATIC__/images/upload.png'}">
                                            <input type="hidden" name="img" value="{$info.img}">
                                            <div class="input-group-btn">
                                                <button onclick="GetUploadify(1,'img','monitor','call_back')"
                                                        class="btn btn-default" type="button">上传图片
                                                </button>
                                            </div>
                                        </div>
                                        <span class="error" id="err_img"></span>
                                    </div>
                                    <div class="form-group col-md-12">
                                        <label class="form-title"><em class="require">*</em>分类排序</label>
                                        <input type="text" class="form-control" name="sort" value="{$info.sort|default=50}" placeholder="请输入分类排序"/>
                                        <span class="help">将按照数字从小到大排序</span>
                                        <span class="error" id="err_sort"></span>
                                    </div>
                                    <input type="hidden" name="is_open" value="{$info.is_open}">
                                    <div class="col-md-12">
                                        <input type="hidden" name="{$info.pk}" value="{$info[$info.pk]}">
                                        <button type="button" class="btn-theme submit-btn">确定</button>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </main>
        <!--End 页面主要内容-->
    </div>
</div>

<script type="text/javascript">
    $(function(){
        $('.submit-btn').click(function () {
            $('span.error').hide();
            $('span.error').parent().removeClass('has-error');
            var url = "{:url('Goods/add_edit_monitor')}";
            var data = $('#submit-form').serialize();
            $.ajax({
                type: "POST",
                url: url,
                data: data,
                dataType: 'json',
                success: function (data) {
                    if (data.code == 1) {
                        showSuccessMsg(data.msg, function () {
                            if (data.data.url) {
                                location.href = data.data.url;
                            }
                        });
                    } else if (data.code == 10) {
                        showErrorMsg(data.msg);
                        $.each(data.data.result, function(index, item) {
                            $('#err_' + index).text(item).show();
                            $('#err_' + index).parent().addClass('has-error');
                        });
                    } else {
                        showErrorMsg(data.msg);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    showErrorMsg("网络失败，请刷新后重试!");
                }
            });
        });
    });

    function call_back(fileurl_tmp, fileurl_id) {
        $("[name=" + fileurl_id + "]").val(fileurl_tmp);
        $("#" + fileurl_id).attr('src', fileurl_tmp);
    }
    // 上传商品相册回调函数
    function call_back2(paths) {
        for (var i = 0; i < paths.length; i++) {
            var html = '<li class="col-xs-4 col-sm-3 col-md-2">' +
                ' <figure>' +
                ' <input type="hidden" name="images[]" value="' + paths[i] + '">' +
                ' <img src="' + paths[i] + '">' +
                ' <figcaption>' +
                ' <a onclick="ClearPicArr2(this,\'' + paths[i] + '\')" class="btn btn-round btn-square btn-danger"><i class="mdi mdi-delete"></i></a>' +
                ' </figcaption>' +
                ' </figure>' +
                '</li>';
            $('.add-pic').before(html);
        }
    }
</script>
</body>
</html>
